package com.imooc.project.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.imooc.project.entity.Customer;
import com.imooc.project.service.CustomerService;
import com.imooc.project.util.ExportUtils;
import com.imooc.project.util.ResultUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhou
 * @since 2021-06-18
 */
@Controller
@RequestMapping("/customer")
//@Api(value = "ExportController", description = "导出管理API")
public class CustomerController {

    //用户信息模版
    private final String USER_INFO_TEMPLATE = "user_info_template.xlsx";

    /**
     * 导出下载工具类
     */
    /**
     * 导出下载工具类
     */
    @Autowired
    private ExportUtils exportUtils;


    @Autowired
    private CustomerService customerService;
    /**
    * @Author weizhen
    * @Description 进入列表页
    * @Data 23:42 2021/6/18
    * @Parms
    * @return
     **/
    @GetMapping("toList")
    public String toList() {
        return "customer/customerList";
    }

    /**
    * @Author weizhen
    * @Description 普通用户进入列表页
    * @Data 15:15 2021/6/19
    * @Parms
    * @return
     **/
    @GetMapping("toList1")
    public String toList1() {
        return "customer/customerList1";
    }

    /**
    * @Author weizhen
    * @Description 查询方法
    * @Data 23:56 2021/6/18
    * @Parms
    * @return
     **/
    @GetMapping("list")
    @ResponseBody
    public R<Map<String, Object>> list(String userName,String phone,Long page,Long limit){

        LambdaQueryWrapper<Customer> wrapper = Wrappers.<Customer>lambdaQuery().like(StringUtils.isNotBlank(userName), Customer::getName, userName)
                .like(StringUtils.isNotBlank(phone), Customer::getPhone, phone)
                .orderByDesc(Customer::getCustomerId);
        Page<Customer> myPage = customerService.page(new Page<>(page, limit), wrapper);
        return ResultUtil.buildPageR(myPage);
    }

    /**
    * @Author weizhen
    * @Description 普通用户查询方法
    * @Data 15:15 2021/6/19
    * @Parms
    * @return
     **/
    @GetMapping("list1")
    @ResponseBody
    public R<Map<String, Object>> list1(String userName,String phone,Long page,Long limit){

        LambdaQueryWrapper<Customer> wrapper = Wrappers.<Customer>lambdaQuery().like(StringUtils.isNotBlank(userName), Customer::getName, userName)
                .like(StringUtils.isNotBlank(phone), Customer::getPhone, phone)
                .orderByDesc(Customer::getCustomerId);
        Page<Customer> myPage = customerService.page(new Page<>(page, limit), wrapper);
        return ResultUtil.buildPageR(myPage);
    }

    /**
     * @Author weizhen
     * @Description 进入新增页
     * @Data 23:42 2021/6/18
     * @Parms
     * @return
     **/
    @GetMapping("toAdd")
    public String toAdd() {
        return "customer/customerAdd";
    }


    // 新增客户
    @PostMapping
    @ResponseBody
    public R<Object> add(@RequestBody Customer customer) {
        return ResultUtil.buildR(customerService.save(customer));
    }


    // 进入更新页
    @GetMapping("toUpdate/{id}")
    public String toUpdate(@PathVariable Long id, Model model) {

        System.out.println("id = " + id);
        Customer customer = customerService.getById(id);
        String  name=customer.getName();
        System.out.println("id = " + name);
        model.addAttribute("customer", customer);
        return "customer/customerUpdate";
    }

    // 修改客户
    @PutMapping
    @ResponseBody
    public R<Object> updateSubmit(@RequestBody Customer customer) {
        return ResultUtil.buildR(customerService.updateById(customer));
    }

    // 删除客户
    @DeleteMapping("/{id}")
    @ResponseBody
    public R<Object> delete(@PathVariable Long id) {

        return ResultUtil.buildR(customerService.removeById(id));
    }

    // 进入详情页
    @GetMapping("toDetail/{id}")
    public String toDetail(@PathVariable Long id, Model model) {

        Customer customer = customerService.getById(id);
        model.addAttribute("customer", customer);
        return "customer/customerDetail";
    }

    /**
     * 客户信息导出
     *
     * @return java.lang.String
     * @author Xing
     * @date 2019-12-04
     */
//    @ApiOperation(value = "客户信息导出", notes = "客户信息导出")
    @GetMapping("/back/soaexport/userInfoExcel")
    @RequiresPermissions("sys:client:export")
    @ResponseBody
    public String userInfoExcel( HttpServletRequest request, HttpServletResponse response) {
        String templateName = this.USER_INFO_TEMPLATE;//模板名称
        String title = "通讯录";//标题
        String newFileName = "通讯录";//文件名
        Integer[] digitPosition = new Integer[]{};
        Map cmap = new HashMap();
        List<Map<String, Object>> mapList = this.customerService.findExcelList(cmap);//数据源 LinkedList
        System.out.println(mapList.get(1));
        this.exportUtils.exportExcel(response, title, templateName, newFileName, mapList, digitPosition, 2);
        System.out.println("ok");
        return "succeed";
    }

}

